//
//  HomePageListFootCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/29.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageListFootCell.h"
#import "HomePageItemModel.h"
#import "RPAppShareView.h"
@interface HomePageListFootCell ()
@property (nonatomic,strong)UILabel *lookCount;
@property (nonatomic,strong)UILabel *shareCount;
@property (nonatomic,strong)UILabel *likeCount;
@property (nonatomic,strong)UILabel *dissCount;
@property (nonatomic,strong)UILabel *commentCount;
@property (nonatomic,strong)UIImageView *shareImgView;
@property (nonatomic,strong)UIImageView *likeImgView;
@property (nonatomic,strong)UIImageView *dissImgView;
@property (nonatomic,strong)UIImageView *commentImgView;
@property (nonatomic,strong)HomePageItemModel *model;
@property (nonatomic,strong)UIButton *likeBtn;
@property (nonatomic,strong)UIButton *shareBtn;
@property (nonatomic,strong)UIButton *dissBtn;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,strong)UIButton *commentBtn;
@property (nonatomic,strong)UIView *line;

@property (nonatomic,strong)UILabel *tagLb;
@property (nonatomic,strong)UIImageView *tagImgView;
@property (nonatomic,strong)UIView *tagView;


@end

@implementation HomePageListFootCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
//        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.tagView = [[UIView alloc]init];
    self.tagView.layer.cornerRadius = 13;
    self.tagView.layer.masksToBounds = YES;
    self.tagView.backgroundColor = RGBA(202, 202, 202, 1);
    [self.contentView addSubview:self.tagView];
    [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(16);
        make.height.mas_equalTo(26);
    }];
    
    // 新增
    self.tagLb = [[UILabel alloc]init];
    self.tagLb.textColor = UIColor.whiteColor;
    self.tagLb.font = font(12);
    self.tagLb.text = @"话题";
    [self.tagView addSubview:self.tagLb];
    
    self.tagImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_page_#_tag"]];
    [self.tagView addSubview:self.tagImgView];
    
    [self.tagImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tagView);
        make.left.equalTo(self.tagView).offset(12);
    }];
    
    [self.tagLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tagView);
        make.left.equalTo(self.tagImgView.mas_right).offset(4);
        make.right.equalTo(self.tagView.mas_right).offset(-12);
    }];
    self.tagView.userInteractionEnabled = NO;
    
    self.lookCount = [[UILabel alloc]init];
    self.lookCount.text = @"5.6w观看";
    self.lookCount.textColor = C102;
    self.lookCount.font = font(12);
    [self.contentView addSubview:self.lookCount];
    [self.lookCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.top.equalTo(self.contentView).offset(64);
    }];
    self.likeCount = [[UILabel alloc]init];
    self.likeCount.textColor = C102;
    self.likeCount.font = font(14);
    [self.contentView addSubview:self.likeCount];
    [self.likeCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-16);
        make.centerY.equalTo(self.lookCount);
    }];
    
    self.likeImgView = [[UIImageView alloc]init];
    self.likeImgView.image = [UIImage imageNamed:@"home_like_tag"];
    self.likeImgView.highlightedImage = [UIImage imageNamed:@"home_likeSelected_tag"];
    [self.contentView addSubview:self.likeImgView];
    [self.likeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeCount.mas_left).offset(-8);
        make.centerY.equalTo(self.lookCount);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    self.dissCount = [[UILabel alloc]init];
    self.dissCount.textColor = C102;
    self.dissCount.font = font(14);
    [self.contentView addSubview:self.dissCount];
    [self.dissCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeImgView.mas_left).offset(-16);
        make.centerY.equalTo(self.lookCount);
    }];
    
    self.dissImgView = [[UIImageView alloc]init];
    self.dissImgView.image = [UIImage imageNamed:@"home_diss_tag"];
    self.dissImgView.highlightedImage = [UIImage imageNamed:@"home_dissSelected_tag"];
    [self.contentView addSubview:self.dissImgView];
    [self.dissImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dissCount.mas_left).offset(-8);
        make.centerY.equalTo(self.lookCount);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    
    self.commentCount = [[UILabel alloc]init];
    self.commentCount.textColor = C102;
    self.commentCount.font = font(14);
    [self.contentView addSubview:self.commentCount];
    [self.commentCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dissImgView.mas_left).offset(-16);
        make.centerY.equalTo(self.lookCount);
    }];
    
    self.commentImgView = [[UIImageView alloc]init];
    self.commentImgView.image = [UIImage imageNamed:@"homePage_comment_tag"];
    [self.contentView addSubview:self.commentImgView];
    [self.commentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentCount.mas_left).offset(-8);
        make.centerY.equalTo(self.lookCount);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    self.shareCount = [[UILabel alloc]init];
    self.shareCount.textColor = C102;
    self.shareCount.font = font(14);
    [self.contentView addSubview:self.shareCount];
    [self.shareCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentImgView.mas_left).offset(-16);
        make.centerY.equalTo(self.lookCount);
    }];
    
    self.shareImgView = [[UIImageView alloc]init];
    self.shareImgView.image = [UIImage imageNamed:@"home_share_tag"];
    [self.contentView addSubview:self.shareImgView];
    [self.shareImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.shareCount.mas_left).offset(-8);
        make.centerY.equalTo(self.lookCount);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
        
    self.likeBtn = [[UIButton alloc]init];
    [self.likeBtn addTarget:self action:@selector(likeMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.likeBtn];
    
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.likeImgView);
        make.left.equalTo(self.likeImgView.mas_left);
        make.right.equalTo(self.likeCount.mas_right);
    }];
    
    self.shareBtn = [[UIButton alloc]init];
    [self.shareBtn addTarget:self action:@selector(shareMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.shareBtn];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.shareImgView);
        make.left.equalTo(self.shareImgView.mas_left);
        make.right.equalTo(self.shareCount.mas_right);
    }];
    
    self.dissBtn = [[UIButton alloc]init];
    [self.dissBtn addTarget:self action:@selector(dissMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.dissBtn];
    
    [self.dissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.dissImgView);
        make.left.equalTo(self.dissImgView.mas_left);
        make.right.equalTo(self.dissCount.mas_right);
    }];
    
    self.commentBtn = [[UIButton alloc]init];
    [self.commentBtn addTarget:self action:@selector(commentMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.commentBtn];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.commentImgView);
        make.left.equalTo(self.commentImgView.mas_left);
        make.right.equalTo(self.commentCount.mas_right);
    }];

    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(248, 248, 248, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(8);
    }];
    
    self.tagView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod)];
    [self.tagView addGestureRecognizer:tap];
}

- (void)tapMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showTopicListViewWithTagModel:)]) {
        [self.delegate showTopicListViewWithTagModel:self.model.tag];
    }
}

- (void)bindViewModel:(HomePageItemModel *)viewModel {
    self.model = viewModel;
    if (viewModel.likeNum.intValue >= 10000) {
        NSInteger count = viewModel.likeNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likeCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likeCount.text = [NSString stringWithFormat:@"%@",viewModel.likeNum];
    }
    
    if (viewModel.unlikeNum.intValue >= 10000) {
        NSInteger count = viewModel.unlikeNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.dissCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.dissCount.text = [NSString stringWithFormat:@"%@",viewModel.unlikeNum];
    }
    
    if (viewModel.shareNum.intValue >= 10000) {
        NSInteger count = viewModel.shareNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.shareCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.shareCount.text = [NSString stringWithFormat:@"%@",viewModel.shareNum];
    }
    
    if (viewModel.watchNum.intValue >= 10000) {
        NSInteger count = viewModel.watchNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.lookCount.text = [NSString stringWithFormat:@"%@w观看",[RPAppManager notRounding:count/10000.0f afterPoint:1]];

    }else{
        if (viewModel.watchNum.intValue == 0) {
            self.lookCount.text = @" ";
        }else{
            self.lookCount.text = [NSString stringWithFormat:@"%@观看",viewModel.watchNum];
        }
    }
    
    if (viewModel.commentNum.intValue >= 10000) {
        NSInteger count = viewModel.commentNum.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.commentCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.commentCount.text = [NSString stringWithFormat:@"%@",viewModel.commentNum.intValue == 0?@0:viewModel.commentNum];
    }
    
    self.likeImgView.highlighted = self.model.isLike;
    self.dissImgView.highlighted = self.model.isUnLike;
    
    if (viewModel.tag != nil) {
        [self.tagView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(26);
        }];
        [self.lookCount mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(64);
        }];
        self.tagLb.text = self.model.tag.tagName;
        self.tagView.hidden = NO;
    }else{
        [self.tagView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [self.lookCount mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(20);
        }];
        self.tagLb.text = @"";
        self.tagView.hidden = YES;
    }
    if (self.model.hideLine) {
        self.line.hidden = YES;
    }else{
        self.line.hidden = NO;
    }
}

- (void)coverMethod{
    
}

- (void)shareMethod{
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(shareWithCurrentModel:)]) {
        [self.delegate shareWithCurrentModel:self.model];
    }
}
- (void)commentMethod{
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentWithCurrentModel:)]) {
        [self.delegate commentWithCurrentModel:self.model];
    }
}

- (void)likeMethod{
    
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    if (![RPAppManager isLogin]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:)]) {
            [self.delegate doDissOrLikeWithStatus:YES];
        }
        return;
    }
    
    
    [MobClick event:@"homePage_like"];
    
    self.model.isLike = !self.model.isLike;
    
    self.likeImgView.highlighted = self.model.isLike;
    
    
    NSInteger max = self.model.likeNum.intValue +  (self.model.isLike?1:-1);
    
    if (max >= 10000) {
        NSInteger count = max;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.likeCount.text =  [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.likeCount.text = [NSString stringWithFormat:@"%ld",(long)max];
    }
    
    self.model.likeNum = @(max);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:)]) {
        [self.delegate doDissOrLikeWithStatus:YES];
    }

}

- (void)dissMethod{
    
    if ([RPAppManager isShowNetworkView]) {
        [WHToast showErrorWithMessage:@"网络好像有点问题，请检查后重试" duration:2 finishHandler:nil];
        return;
    }
    if (![RPAppManager isLogin]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:)]) {
            [self.delegate doDissOrLikeWithStatus:YES];
        }
        return;
    }
    
    [MobClick event:@"homePage_unLike"];
    
    self.model.isUnLike = !self.model.isUnLike;
    
    self.dissImgView.highlighted = self.model.isUnLike;
    
    NSInteger max = self.model.unlikeNum.intValue + (self.model.isUnLike?1:-1);
    
    if (max >= 10000) {
        NSInteger count = max;
        if (count % 1000 == 0) {
            count += 1;
        }
        self.dissCount.text = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        self.dissCount.text = [NSString stringWithFormat:@"%ld",(long)max];
    }
    
    self.model.unlikeNum = @(max);
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(doDissOrLikeWithStatus:)]) {
        [self.delegate doDissOrLikeWithStatus:NO];
    }
}

@end
